//
//  UserProfileViewModel.swift
//  FeastBooking
//
//  Created by Imon on 2017/11/24.
//  Copyright © 2017年 Imon. All rights reserved.
//

import Foundation
import RxSwift
import Moya

class UserProfileViewModel {
    
    private var provider: RxMoyaProvider<UserProfileApi>
    
    init(token: String) {
        let endpointClosure = { (target: UserProfileApi) -> Endpoint<UserProfileApi> in
            let defaultEndpoint = RxMoyaProvider.defaultEndpointMapping(for: target)
            return defaultEndpoint.adding(newHTTPHeaderFields: ["content-type": "application/json", "Authorization": "Bearer \(token)"])
        }
        provider = RxMoyaProvider<UserProfileApi>(endpointClosure: endpointClosure)
    }
    
    func getProfile() -> Observable<UserProfile> {
        return provider.request(.getProfile())
               .mapJSON()
               .mapObject(type: UserProfile.self)
    }
    
    func postAvatar(avatar: UIImage) -> Observable<PostResponse> {
        return provider.request(.postAvatar(avatar: avatar))
               .mapJSON()
               .mapObject(type: PostResponse.self)
    }
    
    func changeEmail(token: String) -> Observable<PostResponse> {
        return provider.request(.changeEmail(emailToken: "Bearer \(token)"))
               .mapJSON()
               .mapObject(type: PostResponse.self)
    }
    
    func changePassword(password: String, newPassword: String) -> Observable<PostResponse> {
        return provider.request(.changePassword(password: password, newPassword: newPassword))
               .mapJSON()
               .mapObject(type: PostResponse.self)
    }
    
    func changePhone(phone: String) -> Observable<PostResponse> {
        return provider.request(.changePhone(phone: phone))
               .mapJSON()
               .mapObject(type: PostResponse.self)
    }
}

